//
//  YAYIDropMenuView.m
//  yayi-医生版
//
//  Created by Veer on 16/4/26.
//  Copyright © 2016年 Veer. All rights reserved.
//

#import "YAYIDropMenuView.h"

static const CGFloat cellHeight = 40.0f;

@implementation YAYIModel

@end


@implementation YAYIMenuCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_width, cellHeight)];
        [self.contentView addSubview:_bgView];
        
        self.point = [[UIView alloc] initWithFrame:CGRectMake(10, (cellHeight - 3)/2.0, 3, 3)];
        self.point.layer.masksToBounds = YES;
        self.point.layer.cornerRadius = 1.5;
        [self.bgView addSubview:_point];
        
        self.selectedIcon = [UIImageView new];
        self.selectedIcon.image = [UIImage imageNamed:@"check_select"];
        self.selectedIcon.frame = CGRectMake(15, (cellHeight - 11) * 0.5, 14.5, 11);
        [self.bgView addSubview:self.selectedIcon];
        self.selectedIcon.hidden = YES;
        
        self.textL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.selectedIcon.frame)+10, 0,300, cellHeight)];
        self.textL.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:_textL];
        
    }
    return self;
}


- (void)setSetSelected:(BOOL)setSelected
{
    
    self.selectedIcon.hidden = !setSelected;
    self.textL.textColor = setSelected ? YAYIColor(94, 165, 237) : YAYIColor(78, 78, 78);
}

@end




@implementation YAYIDropMenuView
{
    UITableView *listTableView;
    NSInteger currentSelect;
    CGRect orginalFrame;
    BOOL isShow;
    UIView *bgView;
}

- (instancetype)initWithFrame:(CGRect)frame showOnView:(UIView*)view AllDataArr:(NSMutableArray*)arr showArr:(NSMutableArray *)showArr
{
    if (self = [super initWithFrame:frame]) {
        self.AllDataArr = arr;
        self.showArr = showArr;
//        if (!showArr) {
//            self.showArr = arr;
//        }
        
        for (int i=0; i < showArr.count; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i*(CGRectGetWidth(frame)/showArr.count), 0, CGRectGetWidth(frame)/showArr.count, CGRectGetHeight(frame))];
            button.tag = 100 + i;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            
//            NSArray *temp = self.showArr[i];
//            YAYIModel *model = [temp firstObject];
            
            button.titleLabel.textAlignment = NSTextAlignmentCenter;
            button.backgroundColor = [UIColor whiteColor];
//            button.layer.borderColor = [UIColor grayColor].CGColor;
//            button.layer.borderWidth = 0.4;
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            
            [button setTitleColor:YAYIColor(100, 100, 100) forState:UIControlStateNormal];
            [button setTitleColor:YAYIColor(94, 165, 237) forState:UIControlStateSelected];
            [button setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateSelected];
            
            CGSize strSize = [showArr[i] sizeWithFont:button.titleLabel.font];
            CGSize curImageSize = button.currentImage.size;
            
            button.imageEdgeInsets = UIEdgeInsetsMake(0, strSize.width+2, 0, -strSize.width-2);
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -curImageSize.width-2, 0, curImageSize.width+2);
            
            [button setTitle:showArr[i] forState:UIControlStateNormal];
            [self addSubview:button];
            [view addSubview:self];
            
            // 竖线
            if (i != arr.count - 1) {
                UIView *line = [UIView new];
                line.backgroundColor = YAYIColor(205, 205, 205);
                [self addSubview:line];
                [line mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(button.mas_centerY);
                    make.left.mas_equalTo(button.mas_right).with.offset(-1);
                    make.size.mas_equalTo(CGSizeMake(0.5, 20));
                }];
            }
            
            //
            currentSelect = 0;
//            [self selectClum:i Row:0];
            
        }
        
        // 线
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = YAYICellLineColor;
        lineView.frame = CGRectMake(0, CGRectGetHeight(frame)-0.5, SCREEN_width, 0.5);
        [self addSubview:lineView];
        
        // tableview
        listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame), SCREEN_width, SCREEN_height - CGRectGetMaxY(self.frame))];
        orginalFrame = CGRectMake(0, CGRectGetMaxY(frame), SCREEN_width, 0);
        listTableView.delegate = self;
        listTableView.dataSource = self;
        listTableView.hidden = YES;
        listTableView.separatorColor = YAYICellLineColor;
        //ChdTable.backgroundColor = [UIColor redColor];
        

        bgView = [[UIView alloc] initWithFrame:orginalFrame];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [tap addTarget:self action:@selector(bgViewClick:)];
        [bgView addGestureRecognizer:tap];
        bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [view addSubview:bgView];
        
        if ([ listTableView respondsToSelector:@selector(setSeparatorInset:)]) {
            
            [listTableView   setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            
        }
        if ([listTableView respondsToSelector:@selector(setLayoutMargins:)]) {
            
            [listTableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
            
        }
        
        [view addSubview:listTableView];
        self.backgroundColor = [UIColor orangeColor];
    }
    
    return self;
}

- (void)refreshData:(NSMutableArray *)allDataArray
{
    self.AllDataArr = allDataArray;
    [self realShow];
}


- (void)realShow
{
    
    
    listTableView.hidden = YES;
    listTableView.mj_h = cellHeight * [_AllDataArr[currentSelect] count];
    [listTableView reloadData];
    
    bgView.mj_h = SCREEN_height - CGRectGetMaxY(self.frame);
    
    listTableView.frame = orginalFrame;
    listTableView.hidden = NO;
    
    UIButton *btn = [self viewWithTag:currentSelect + 100];//self.subviews[currentSelect];
    
    [UIView animateWithDuration:0.2 animations:^{
        if (cellHeight * [_AllDataArr[currentSelect] count]>SCREEN_height-CGRectGetMaxY(self.frame)) {
            listTableView.mj_h = SCREEN_height - CGRectGetMaxY(self.frame);
        }else{
            listTableView.mj_h = cellHeight * [_AllDataArr[currentSelect] count];
        }
        
        btn.selected = YES;
//        btn.imageView.transform = CGAffineTransformMakeRotation(0);
    }];
}

- (void)hideCurrent
{
    
    UIButton *btn = [self viewWithTag:100 + currentSelect];//self.subviews[currentSelect];
    btn.selected = NO;
    [UIView animateWithDuration:0.2 animations:^{
        listTableView.frame = orginalFrame;
//        btn.imageView.transform = CGAffineTransformMakeRotation(M_PI);
    }];
    bgView.frame = orginalFrame;
}


#pragma mark - 点击事件
- (void)bgViewClick:(UITapGestureRecognizer*)tap
{
    [self hideCurrent];
    isShow = NO;
    bgView.frame = orginalFrame;
}


- (void)buttonClick:(UIButton *)button
{
    if (button.tag - 100 == currentSelect) {
        if (isShow) {
            [self hideCurrent];
        }else{
            [self showIndex:button.tag - 100];
        }
        isShow = !isShow;
    }else{
        [self showIndex:button.tag - 100];
        isShow = YES;
    }
}

- (void)showIndex:(NSInteger)index
{
    if (currentSelect != index) {
        UIButton *btn = [self viewWithTag:currentSelect + 100];//self.subviews[currentSelect]
        btn.selected = NO;
        [UIView animateWithDuration:0.2 animations:^{
            //            btn.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        }];
    }
    
    
    currentSelect = index;
    
    if ([self.delegate respondsToSelector:@selector(selectColum:)]) {
        [self.delegate selectColum:currentSelect];
    }
    [self realShow];
}


- (void)selectClum:(NSInteger)colum Row:(NSInteger)row
{
    //默认选中第一个
    NSArray *temp = self.AllDataArr[colum];
    for (int i=0; i<temp.count; i++) {
        YAYIModel *model = temp[i];
        if (i == row) {
            model.isSelect = YES;
        }else{
            model.isSelect = NO;
        }
    }
    //[ChdTable reloadData];
    
    UIButton *btn = [self viewWithTag:100 + colum];//(UIButton *)arr[colum];
    YAYIModel *model = self.AllDataArr[colum][row];
    
    // 最后一项不改变按钮文字
    if (2 != colum) {
        [btn setTitle:model.name forState:UIControlStateNormal];
        
        CGSize strSize = [model.name sizeWithFont:btn.titleLabel.font];
        CGSize curImageSize = btn.currentImage.size;
        
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, strSize.width+2, 0, -strSize.width-2);
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, -curImageSize.width-2, 0, curImageSize.width+2);
    }
    
}


#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_AllDataArr[currentSelect] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *chdResuseID = @"CHD_RESUSE";
    
    YAYIMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:chdResuseID];
    
    if (cell == nil) {
        cell = [[YAYIMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:chdResuseID];
    }
    
    YAYIModel * model = [_AllDataArr[currentSelect] objectAtIndex:indexPath.row];
    cell.textL.text = model.name;

    if (model.isSelect) {
        cell.setSelected = YES;
    }else{
        cell.setSelected = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    //*************  本段代码用于选择相同选项时不在回调，不需要可注掉  ****************
    YAYIModel *modelLast = self.AllDataArr[currentSelect][indexPath.row];
    if (modelLast.isSelect) {
        [self hideCurrent];
        isShow = NO;
        
        return;
    }
    //*************  本段代码用于选择相同选项时不在回调，不需要可注掉  ****************
    [self selectClum:currentSelect Row:indexPath.row];
    
    [self hideCurrent];
    isShow = NO;
    YAYIModel *model = self.AllDataArr[currentSelect][indexPath.row];
    if ([self.delegate respondsToSelector:@selector(selectColum:Row:Model:)]) {
        [self.delegate selectColum:currentSelect Row:indexPath.row Model:model];
    }
    
}



@end
